package com.gaofans.vertx.boot.common.config;

import com.gaofans.vertx.boot.common.utils.CommonConstant;
import com.gaofans.vertx.boot.common.verticle.*;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.spi.VerticleFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 在容器中创建一个vertx对象
 * @author GaoFans
 */
@Configuration(proxyBeanMethods = false)
public class VertxAutoConfig {

    @ConfigurationProperties(CommonConstant.VERTX_CORE_PREFIX)
    @Bean
    public VertxOptions vertxOptions(){
        return new VertxOptions();
    }

    @Bean
    public Vertx vertx(VertxOptions vertxOptions){
        return Vertx.vertx(vertxOptions);
    }

    @Bean
    public VerticleInstanceScope verticleInstanceScope(Vertx vertx){
        return new VerticleInstanceScope(vertx);
    }

    @Bean
    public NoneResultHandlerBuilder noneResultHandlerBuilder(){
        return new NoneResultHandlerBuilder();
    }

    @Bean
    public DefaultDeploymentOptionsBuilder defaultDeploymentOptionsBuilder(){
        return new DefaultDeploymentOptionsBuilder();
    }

    @Bean
    @ConditionalOnMissingBean
    public SpringVerticleFactory springVerticleFactory(Environment env){
        return new SpringVerticleFactory(env.getProperty("spring.application.name",""));
    }

    @Bean
    @ConditionalOnMissingBean
    public VerticleDiscoverer verticleDiscoverer(){
        return new AnnotationVerticleDiscoverer();
    }

    @Bean
    public MainVerticleLifecycle mainVerticleLifecycle(Vertx vertx, VerticleFactory factory,VerticleDiscoverer discoverer){
        return new MainVerticleLifecycle(vertx,factory,discoverer);
    }
}
